package pearson.automation.forms.courseDetails;

import java.util.Calendar;

import org.openqa.selenium.By;

import framework.BaseForm;
import framework.elements.Button;
import framework.elements.DateCalendar;
import framework.elements.Label;
import framework.elements.TextBox;
import framework.elements.ComboBox;

public class CoursePublish extends BaseForm {
    private TextBox  title = new TextBox(By.id("course_title"), "Title");
    private TextBox  edition = new TextBox(By.id("course_edition"), "Edition");
    private TextBox  subject = new TextBox(By.id("course_subject"), "Subject");
    private ComboBox term = new ComboBox(By.id("term"), "Term");
    private ComboBox privacy = new ComboBox(By.id("privacy"), "Privacy");
    private TextBox  startDate = new TextBox(By.id("start_date"), "Start Date");
    private DateCalendar calendar = new DateCalendar(By.tagName("button"), "button");
    private TextBox  description = new TextBox(By.tagName("textarea"), "Description");
    private TextBox  tagsInput = new TextBox(By.className("textboxlist-bit-editable-input"), "Tags Input");
    private Label    tagsOutput = new Label(By.className("textboxlist-bits"), "Tags Output");
    private Button   tagDeleteButton = new Button(By.className("textboxlist-bit-box-deletebutton"), "Tag Delete button");
      
    private static Button next = new Button(By.name("_submit"), "Next");
    private static Button close = new Button(By.linkText("Close"), "Close");
    
    public CoursePublish() {
        super(next.getLocator(), "Publish");
    }
    
    public void setTitle(String title) {
        this.title.setText(title);
    }
    
    public String getTitle() {
        return title.getValue();
    }
    
    public void setEdition(String edition) {
        this.edition.setText(edition);
    }
    
    public String getEdition() {
        return edition.getValue();
    }
    
    public void setSubject(String subject) {
        this.subject.setText(subject);
    }
    
    public String getSubject() {
        return subject.getValue();
    }
    
    public void setTerm(String term) {
        this.term.selectByText(term);
    }
    
    public String getTerm() {
        return term.getSelectedLabel();
    }
    
    public void setPrivacy(String privacy) {
        this.privacy.selectByText(privacy);
    }
    
    public String getPrivacy() {
        return privacy.getSelectedLabel();
    }
    
    public String getStartDate() {
        return startDate.getValue();
    }
    
    public void setDescription(String description) {
        this.description.setText(description);
    }
    
    public String getDescription() {
        return description.getValue();
    }
    
    public void setTags(String tags) {
        //Clear all tags here
        while (tagDeleteButton.isPresent()) {
            tagDeleteButton.clickAndWait();
        }
        tagsInput.setText(tags);
    }
    
    public String getTags() {
        return tagsOutput.getText().replace("\n", ", ");
    }
    
    public CoursePublishingDetails clickNext() {
        next.clickAndWait();
        return new CoursePublishingDetails();
    }
    
    public CourseForm clickClose() {
        close.clickAndWait();
        return new CourseForm();
    }

    public void setValidDate(Calendar date) {
        calendar.selectDate(date);
    }
}
